<template>
	<view class="w-1 p-30">
		<view class="bg-ff br-20">
			<view class="fr-s-c bb-solid p-30" @click="chooseImage">
				<text class="c-33 fs-30 bold flex-1">头像</text>
				<image class="circle" style="height: 92rpx; width: 92rpx;" :src="user.avatar || '/static/logo.png'"
					mode="aspectFill"></image>
				<up-icon name="arrow-right"></up-icon>
			</view>

			<view class="w-1 relative p-30 bb-solid fr-s-c" @click="shownick">
				<text class="c-33 bold fs-30" style="width: 176rpx;">昵称</text>
				<input v-model.trim="user.nickname" type="text" placeholder="请输入昵称" class="flex-1 t-r fs-28" />
				<up-icon name="arrow-right"></up-icon>
				<view class="absolute-0 w-1 h-1" style="z-index: 9;"></view>
			</view>

			<view class="w-1 relative p-30 bb-solid fr-s-c" @click="showact">
				<text class="c-33 bold fs-30" style="width: 176rpx;">性别</text>
				<input type="text" placeholder="请选择性别" :value="itemList[curs]" class="flex-1 t-r fs-28" />
				<up-icon name="arrow-right"></up-icon>
				<view class="w-1 h-1 absolute-0" style="z-index: 2;"></view>
			</view>

			<picker class="w-1 bb-solid" mode="date" :value="user.birthday" @change="changeDate">
				<view class="w-1 p-30 relative fr-s-c">
					<text class="c-33 bold fs-30" style="width: 176rpx;">出生日期</text>
					<input type="text" :value="user.birthday" placeholder="请选择出生日期" class="flex-1 t-r fs-28" />
					<up-icon name="arrow-right"></up-icon>
					<view class="w-1 h-1 absolute-0" style="z-index: 2;"></view>
				</view>
			</picker>

			<block v-if="user.starf_status==1">
				<view class="w-1 relative p-30 bb-solid fr-s-c" @click="previewImage(1)">
					<text class="c-33 bold fs-30" style="width: 176rpx;">身份证照</text>
					<input type="text" placeholder="" class="flex-1 t-r fs-28" />
					<up-icon name="arrow-right"></up-icon>
					<view class="absolute-0 w-1 h-1" style="z-index: 9;"></view>
				</view>
				<view class="w-1 bb-solid relative p-30  fr-s-c" @click="previewImage(0)">
					<text class="c-33 bold fs-30" style="width: 176rpx;">证件</text>
					<input type="text" placeholder="" class="flex-1 t-r fs-28" />
					<up-icon name="arrow-right"></up-icon>
					<view class="absolute-0 w-1 h-1" style="z-index: 9;"></view>
				</view>
			</block>

			<view class="w-1 relative p-30  fr-s-c" v-else @click="toahtu">
				<text class="c-33 bold fs-30" style="width: 176rpx;">成为师傅</text>
				<!-- 0=未认证,1=认证成功,2=待审核', -->
				<input type="text"
					:value="user.starf_status==0?'未认证':user.starf_status==1?'已认证':user.starf_status==2?'审核中':'审核失败'"
					placeholder="" class="flex-1 t-r fs-28" />
				<up-icon name="arrow-right"></up-icon>
				<view class="absolute-0 w-1 h-1" style="z-index: 9;"></view>
			</view>
		</view>


		<!-- <view class="iphonex">
			<view class="w-1 p-30 fr-c">
				<view class="big-btn fr-c" style="width: 690rpx;" @click="sub">确认修改</view>
			</view>
		</view> -->
	</view>
</template>

<script setup>
	const itemList = ['女', '男'];
	const curs = ref(-1);

	// function changeSex(index){
	// 	curs.value = index;

	// }


	function toahtu() {
		if (user.value.starf_status == 0 || user.value.starf_status == 3) {
			uni.navigateTo({
				url: '/pagemaster/pages/Login/AuthName'
			})
		} else if (user.value.starf_status == 1) {
			uni.$u.toast('已认证')
		} else if (user.value.starf_status == 2) {
			uni.$u.toast('审核中，请你心等待~')
		}
	}

	function shownick() {
		uni.showModal({
			title: '昵称',
			editable: true,
			placeholderText: user.value.nickname || '请输入昵称',
			success: (ret) => {
				if (ret.confirm) {
					if (!ret.content.trim().length) return uni.$u.toast('请输入昵称');
					uni.$post({
						url: '/api/user/Upmyinfo',
						data: {
							nickname: ret.content,
						},
					}).then(res => {
						user.value.nickname = ret.content;
					})
				}
			}
		})
	}


	function previewImage(type) {
		// 0 证件照 1 身份证照	
		if (type == 0 && !user.value.starf_yyzzimg) return uni.$u.toast('您还未上传证件照');
		if (type == 1 && !user.value.starf_zcardimg) return uni.$u.toast('您还未上传身份证照');
		uni.previewImage({
			current: 0,
			urls: type == 0 ? [uni.$dimg(user.value.starf_yyzzimg)] : [uni.$dimg(user.value.starf_zcardimg), uni
				.$dimg(user.value.starf_bcardimg)
			]
		})
	}

	function showact() {
		uni.showActionSheet({
			itemList: itemList,
			success: (ret) => {
				curs.value = ret.tapIndex
				uni.$post({
					url: '/api/user/Upmyinfo',
					data: {
						gender: ret.tapIndex,
					},
				}).then(res => {
					user.value.gender = ret.tapIndex;
				})
			}
		})
	}


	function changeDate(e) {
		console.log(e.detail.value)
		uni.$post({
			url: '/api/user/Upmyinfo',
			data: {
				birthday: e.detail.value,
			},
		}).then(res => {
			user.value.birthday = e.detail.value;
		})
	}

	const user = ref('')
	onShow(() => {
		getuser()
	})
	const getuser = () => {
		uni.$post({
			url: '/api/User/Userinfo',
			loading: false
		}).then(res => {
			for (let i in res.data) {
				if (res.data[i] == null) {
					res.data[i] = ''
				}
			}
			user.value = res.data;
			curs.value = res.data.gender;
		})
	}






	function chooseImage() {
		uni.chooseImage({
			count: 1, //默认9
			sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
			success: async (res) => {
				const avatar = res.tempFilePaths
				const imageres = await uni.$up({
					imageUrl: avatar
				})
				uni.$post({
					url: '/api/user/Upmyinfo',
					data: {
						avatar: imageres[0].fullurl,
					}
				}).then(res => {
					user.value.avatar = imageres[0].fullurl
					uni.hideLoading()
				})

			}
		});
	}

	function sub() {
		if (!(user.value.nickname.trim().length)) return uni.$u.toast('请输入昵称');
		// if(!(user.value.realname.trim().length)) return uni.$u.toast('请输入姓名');
		uni.$post({
			url: '/api/user/Upmyinfo',
			data: {
				avatar: user.value.avatar,
				nickname: user.value.nickname,
				// realname:user.value.realname,
				gender: user.value.gender,
				birthday: user.value.birthday
			},
		}).then(res => {
			getApp().tt(res.msg, 'success')
			uni.$delayBack()
		})
	}
</script>

<style>
	page {
		background: #f1f5f6;
	}
</style>